package com.google.devrel.example.android.protips.mvp.view;

import java.text.MessageFormat;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.devrel.example.android.protips.R;
import com.google.devrel.example.android.protips.mvp.IMainView;
import com.google.devrel.example.android.protips.mvp.MainPresenter;

public class MainTabletActivityView extends Activity implements IMainView {

	MainPresenter _presenter;

	public MainTabletActivityView() {
		_presenter = new MainPresenter(this);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tablet);
		_presenter.setupSensor();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		_presenter.clearSensors();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		return buildMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		if (handleMenuItemSelected(featureId, item))
			return true;

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean buildMenu(Menu menu) {
		menu.add(0, MENUITEM_TRACK, 0, R.string.menu_track).setShowAsAction(
				MenuItem.SHOW_AS_ACTION_IF_ROOM
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		menu.add(0, MENUITEM_MONITOR, 0, R.string.menu_monitor)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		menu.add(0, MENUITEM_SERVICE, 0, R.string.menu_service)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		menu.add(0, MENUITEM_MISC, 0, R.string.menu_misc).setShowAsAction(
				MenuItem.SHOW_AS_ACTION_IF_ROOM
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		return true;
	}

	@Override
	public boolean handleMenuItemSelected(int featureId, MenuItem item) {
		Fragment fragment = null;

		switch (item.getItemId()) {
		case MENUITEM_TRACK:
			fragment = new TrackTabletFragmentView();
			break;
		case MENUITEM_MONITOR:
			fragment = new MonitorTabletFragmentView();
			break;
		case MENUITEM_SERVICE:
			fragment = new ServiceTabletFragmentView();
			break;
		case MENUITEM_MISC:
			fragment = new MiscTabletFragmentView();
			break;
		}

		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();

		// Replace whatever is in the fragment_container view with this
		// fragment,
		// and add the transaction to the back stack
		transaction.replace(R.id.container, fragment);
		transaction.addToBackStack(null);

		// Commit the transaction
		transaction.commit();
		return true;
	}

	@Override
	public void setOrientationText(String disp, String physic) {

		final TextView viewDisplayOrientation = (TextView) findViewById(R.id.tv_disp_orientation);
		final TextView viewPhysicalOrientation = (TextView) findViewById(R.id.tv_phy_orientation);

		viewDisplayOrientation.setText(MessageFormat.format("{0}: {1}",
				getResources().getString(R.string.display_orientation), disp));
		viewPhysicalOrientation
				.setText(MessageFormat.format("{0}: {1}", getResources()
						.getString(R.string.physical_orientation), physic));
	}
}
